package com.farmer.announcement.controller;

import com.farmer.announcement.service.AnnouncementService;
import com.farmer.common.entity.Announcement;
import com.farmer.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 公告控制器
 */
@Slf4j
@RestController
@RequestMapping("/announcement")
@Tag(name = "公告管理", description = "系统公告和新闻相关接口")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 获取公告列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取公告列表", description = "分页获取公告列表")
    public Result<?> getAnnouncementList(@RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        @RequestParam(required = false) String type,
                                        @RequestParam(required = false) String keyword,
                                        @RequestParam(required = false) Integer isPublished) {
        return announcementService.getAnnouncementList(pageNum, pageSize, type, keyword, isPublished);
    }

    /**
     * 获取公告详情
     */
    @GetMapping("/{announcementId}")
    @Operation(summary = "获取公告详情", description = "根据公告ID获取公告详细信息")
    public Result<?> getAnnouncementById(@PathVariable Long announcementId) {
        return announcementService.getAnnouncementById(announcementId);
    }

    /**
     * 添加公告
     */
    @PostMapping("/add")
    @Operation(summary = "添加公告", description = "创建新的公告")
    public Result<?> addAnnouncement(@Valid @RequestBody Announcement announcement) {
        log.info("添加公告请求：{}", announcement.getTitle());
        return announcementService.addAnnouncement(announcement);
    }

    /**
     * 更新公告
     */
    @PutMapping("/update")
    @Operation(summary = "更新公告", description = "更新公告信息")
    public Result<?> updateAnnouncement(@Valid @RequestBody Announcement announcement) {
        log.info("更新公告请求：{}", announcement.getTitle());
        return announcementService.updateAnnouncement(announcement);
    }

    /**
     * 删除公告
     */
    @DeleteMapping("/{announcementId}")
    @Operation(summary = "删除公告", description = "根据公告ID删除公告")
    public Result<?> deleteAnnouncement(@PathVariable Long announcementId) {
        log.info("删除公告请求，公告ID：{}", announcementId);
        return announcementService.deleteAnnouncement(announcementId);
    }

    /**
     * 发布/取消发布公告
     */
    @PutMapping("/publish/{announcementId}")
    @Operation(summary = "发布公告", description = "发布或取消发布公告")
    public Result<?> publishAnnouncement(@PathVariable Long announcementId, @RequestParam Integer isPublished) {
        log.info("发布公告请求，公告ID：{}，状态：{}", announcementId, isPublished);
        return announcementService.publishAnnouncement(announcementId, isPublished);
    }

    /**
     * 获取最新公告
     */
    @GetMapping("/latest")
    @Operation(summary = "获取最新公告", description = "获取最新发布的公告列表")
    public Result<?> getLatestAnnouncements(@RequestParam(defaultValue = "5") Integer limit) {
        return announcementService.getLatestAnnouncements(limit);
    }

    /**
     * 增加浏览次数
     */
    @PutMapping("/view/{announcementId}")
    @Operation(summary = "增加浏览次数", description = "用户查看公告时增加浏览次数")
    public Result<?> incrementViewCount(@PathVariable Long announcementId) {
        return announcementService.incrementViewCount(announcementId);
    }

    /**
     * 健康检查
     */
    @GetMapping("/health")
    @Operation(summary = "健康检查", description = "检查服务是否正常运行")
    public Result<?> health() {
        return Result.success("公告服务运行正常");
    }
} 